<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['show_setmobile']=cfc('core')->check_setmobile(2);
$return['show_setmember']=cfc('core')->check_setmember(2);
set_html('title','订单详情');
if($_RQ['op']=='main'){
	if($_RQ['id']){
		$id = intval($_RQ['id']);
		$order=pdo_get('jy_kabao_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}
	if(!$order){
		exi('指定订单不存在！','error','goback');
	}
	if($order['mid']!=$_SESSION['uid']){
		exi('该礼包不是你的！','error','goback');
	}
	$order['statustitle']=$this->orderstatus[$order['status']];;
	$order['createtime']=date('Y-m-d H:i:s',$order['createtime']);
	//用户地址
	$order['address']=json_decode($order['address'],true);
	if($order['address'] && ($order['address']['address'] || $order['address']['mobile'])){
		$address=$order['address'];
	}else{
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
	}
	$address['pcd']=json_decode($address['pcd'],true);
	$address['diyfields']=json_decode($address['diyfields'],true);
	$order['addresstext']=model('core/member')->get_address_str($address);
	$order['jiangs']=json_decode($order['jiangs'],true);
	//获取地址
	$order['address']=json_decode($order['address'],true);
	if($order['address'] && ($order['address']['address'] || $order['address']['mobile'])){	
			$address=$order['address'];
	}else{
			$address=$order['addressid'];
	}
	$order['addressinfo']=model('core/member')->get_address_str($address);
	$order['code']=pdo_get('jy_kabao_batch_code',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['codeid']));
	if($order['listid']){
		$order['kabao']=pdo_get('jy_kabao_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['listid']),array('id','image','title','desc','price'));
	}else{
		$order['kabao']=json_decode($order['diyinfo'],true);
	}
	if($order['storeid']){
			$order['store']=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['storeid']),array('id','title'));
	}
	//卡密
	$return['virtual']=pdo_getall('jy_weishop_order_virtual',array('orderid'=>$order['id']));
	if($return['virtual']){
		foreach($return['virtual'] as &$vv){
			$tempinfo=pdo_get('jy_weishop_virtual_temp',array('id'=>$vv['virtualid'],'uniacid'=>$_SESSION['uniacid']),array('id','linktitle','linkurl'));
			$vv['linkurl']=$tempinfo['linkurl'];
			$vv['linktitle']=$tempinfo['linktitle'];
		}
	}
	$return['address']=$address;
	$order_verify=pdo_getall('jy_weishop_order_verify',array('orderid'=>$order['id']));
	if($order_verify){
			foreach($order_verify as &$verify){
				$verify['verifycode']=$verify['verify_code'];
				$code_endtime=$verify['endtime']?date('Y-m-d',$verify['endtime']):'';
				if($verify['status']==0 && $verify['endtime'] && $verify['endtime']<TIMESTAMP){
					$verify['status']=2;
					pdo_update('jy_weishop_order_verify',array('status'=>2),array('id'=>$verify['id']));
				}else{
					$verify['url']=custom_url('jy_weishop/verify.detail//id='.$order['id'].'&verifycode='.$verify['verify_code'],true);
					//小程序二维码：
					$url='/jy_weishop/verify/detail/index?id='.$order['id'].'&verifycode='.$verify['verify_code'];
					$pid='verifyid'.$order['id'].'c'.$verify['verify_code'];
					$verify['appcode'] = model('jy_weishop/model')->get_wxappewm($url,$pid);
				}
			}
	}
	$return['order_verify']=$order_verify;
	$goods_verify=pdo_getall('jy_weishop_verifygoods',array('orderid'=>$order['id']));
	if($goods_verify){
			foreach($goods_verify as &$gvv){
				if (!(empty($gvv['limitnum']))){
					$verifygoodlogs = pdo_getall('jy_weishop_verifygoods_log',array('verifygoodsid'=>$gvv['id']));
					$verifynum = 0;
					foreach ($verifygoodlogs as &$verifygoodlog) 
					{
						$verifygoodlog['verifydate']=date('Y-m-d H:i:s',$verifygoodlog['verifydate']);
						$verifynum += intval($verifygoodlog['verifynum']);
					}
					$gvv['verifygoodlogs']=$verifygoodlogs;
					$gvv['lastverifys'] = intval($gvv['limitnum']) - $verifynum;
				}
				if (empty($gvv['limittype'])) {
						$gvv['limitdays']=$gvv['limitdays']?:365;
						$limitdate = intval($gvv['starttime']) + (intval($gvv['limitdays']) * 86400);
				}else{
						$limitdate = intval($gvv['limitdate']);
				}
				if ($limitdate < time()) {
						$gvv['status']=3;
				}
				$code_endtime=$limitdate?date('Y-m-d',$limitdate):'';
				$gvv['url']=custom_url('jy_weishop/verify.verifygoodsdetail//id='.$order['id'].'&verifycode='.$gvv['verifycode'],true);
				//小程序二维码：
				if($_SESSION['fromh5wxapp']){
					//不能超128字符
					$url='/jy_weishop/verify/verifygoodsdetail/index?id='.$order['id'].'&verifycode='.$gvv['verifycode'];
					$url=urlencode($url);
					$url='/pages/html/html?url='.$url;
				}else{
					$url='/jy_weishop/verify/verifygoodsdetail/index?id='.$order['id'].'&verifycode='.$gvv['verifycode'];
				}
				$pid='verifygoodsid'.$order['id'].'c'.$gvv['verifycode'];
				$gvv['appcode'] = model('jy_weishop/model')->get_wxappewm($url,$pid);
			}
	}
	$return['goods_verify']=$goods_verify;
	$return['code_endtime']=$code_endtime;
	if(count($return['virtual']) || count($return['goods_verify']) || count($return['order_verify'])){
		$order['isverify']=2;
	}
	if($order['paytype']){
		$order['paytypetitle']=get_pay_types()[$order['paytype']];
	}
	if($order['sendtype']==2){
		$order['sendtypetitle']='自提/店内';
	}elseif($order['sendtype']==3){
		$order['sendtypetitle']='虚拟商品';
	}
	foreach($order['jiangs'] as &$sv){
		$sv['goods']['image']=tomedia($sv['goods']['image']);
	}
	$return['order']=$order;
	$return['post']=array('libaos'=>'');
	if(!$order['status']){
		$sharelink='jy_shoplibao/songget//frommid='.$_SESSION['uid'].'&orderid='.$id;
		$return['sharedata']=array(
				'title'=>'好友赠送：'.$order['kabao']['title'],
				'desc'=>'点击领取：'.$order['kabao']['title'],
				'link'=>custom_url($sharelink,true),
				'imgUrl'=>tomedia($order['kabao']['image'])
		);
	}
	exi($return);
}
//兑换
if($_RQ['op']=='post'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$id=intval($_RQ['id']);
	model('jy_kabao/order')->get_jiangs($id);
	exi('已领取!','','jy_shoplibao/orderlist/');
}
//确认订单
if($_RQ['op']=='confirm'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$id=intval($_RQ['id']);
	model('jy_weishop/order')->order_receive($id);
	exi('已确认收货!','','jy_shoplibao/orderlist/');
}
if($_RQ['op']=='upposter'){
	$orderid=intval($_RQ['orderid']);
	$url=custom_url('jy_shoplibao/songget//orderid='.$orderid.'&frommid='.$_SESSION['uid'],true);
	$qr=cfc('tools')->createqrcode($url,'250',true);
	$qr=tomedia($qr);
	$image='oss/'.$_SESSION['uniacid'].'/images/jy_shoplibao/orderid'.$orderid.'.jpg';
	$isadd=cfc('files')->source_check($image);
	if(!$isadd && $_SESSION['wxapp_id']){
		$wxappurl='/jy_shoplibao/songget/index?orderid='.$orderid.'&frommid='.$_SESSION['uid'];
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
		$res=model('core/wxapp')->appcode($wxappurl,$wxapp['appid'],$wxapp['appsecret'],200);
		$path='oss/'.$_SESSION['uniacid'].'/images/jy_shoplibao/';
		if(!file_exists(ROOT_AT.$path)){
			mkdir(ROOT_AT.$path,0700, true);
		}
		$filename=$path.'/orderid'.$orderid.'.jpg';
		$qr=cfc('files')->source_save($res,'image','png','二维码',$filename,'',$wxappurl);
	}
	$order=pdo_get('jy_kabao_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
	if($order['listid']){
		$order['kabao']=pdo_get('jy_kabao_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['listid']),array('id','image','title','desc','price'));
	}else{
		$order['kabao']=json_decode($order['diyinfo'],true);
	}
	$posterset=$this->plugin_setting('poster');
	$posterset['data']['height']=300;
	foreach($posterset['data']['data'] as &$dv){
		if($dv['type']=='title'){
			$dv['type']='text';
			$dv['value']=$order['kabao']['title'];
		}
		if($dv['type']=='desc'){
			$dv['type']='text';
			$dv['value']=$order['kabao']['desc'];
		}
	}
	$order['lognum2']=pdo_count('jy_kabao_songlog',array('status'=>2,'uniacid'=>$_SESSION['uniacid'],'orderid'=>$orderid));
	$count=pdo_count('jy_kabao_songlog',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$orderid));
	$order['lslognum']=$order['total']-$count+$order['lognum2'];
	if($order['lslognum']<=0){
		exi('不能再生成卡片了！','error');
	}
	$sn=rand(10000,99999);
	$posterset['data']['data'][]=array('left'=>'10','top'=>'125','type'=>'text','width' =>'120','height' =>'25','size'=>'12','value' =>'卡号:'.$sn,'color'=>'#f2f2f2');
	$poster=plugin('jy_poster')->get_active('','',$orderid.'_'.$sn,$openid,$qr,$posterset['data'],'jy_shoplibao');
	$poster['posterimg']=$poster['poster'];
	model('jy_kabao/order')->upsonglog($orderid,$poster['posterimg'],'',$sn);
	exi($poster);
}
?>